package Managers;

import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

import javax.swing.DefaultListModel;


public class Day
{
	private final Calendar today;
	private boolean[] daymask;
	private List<Appointment> apps;
	private String[] times = new String[]{"07:00","07:30","08:00","08:30","09:00","09:30","10:00","10:30","11:00","11:30","12:00","12:30","13:00","13:30","14:00","14:30","15:00","15:30","16:00","16:30","17:00","17:30","18:00","18:30","19:00","19:30","20:00","20:30","21:00","21:30","22:00","22:30","23:00","23:30"};
	
	public Day(int y, int m, int d)
	{
		apps = new LinkedList<Appointment>();
		today = Calendar.getInstance();
		today.clear();
		today.set(y,m,d);
		daymask = new boolean[34];
	}
	
	public boolean containAppointment(Appointment a)
	{
		for(Appointment ap : apps)
		{
			if(ap.compareTo(a) == 0)
				return true;
		}
		
		return false;
	}
	public void deleteAppointment(Appointment a)
	{
		apps.remove(a);
		
		int h1 = a.getStart().get(Calendar.HOUR);
		int m1 = a.getStart().get(Calendar.MINUTE);
		int h2 = a.getFinish().get(Calendar.HOUR);
		int m2 = a.getFinish().get(Calendar.MINUTE);
		//parse it into a string format
		String s1 = String.format("%02d:%02d", h1, m1);
		String s2 = String.format("%02d:%02d", h2, m2);
		boolean mark = false;
		//update the mask list
		for(int i = 0; i < times.length; i++)
		{
			if(times[i].equalsIgnoreCase(s1))
			{
				mark = true;//there an apointment
			}
			
			if(times[i].equalsIgnoreCase(s2))
			{
				mark = false; //no apointment
			}
			
			//while theres an apointment we want to mark it in the mask 
			if(mark) 
			{
				daymask[i] = true;
			}

		}
		
	}
	
	//add an apointment 
	public void addAppointment(Appointment a)
	{
		//add to apointments
		apps.add(a);
		//get hour and minute compnents
		int h1 = a.getStart().get(Calendar.HOUR);
		int m1 = a.getStart().get(Calendar.MINUTE);
		int h2 = a.getFinish().get(Calendar.HOUR);
		int m2 = a.getFinish().get(Calendar.MINUTE);
		//parse it into a string format
		String s1 = String.format("%02d:%02d", h1, m1);
		String s2 = String.format("%02d:%02d", h2, m2);
		
		
		boolean mark = false;
		//update the mask list
		for(int i = 0; i < times.length; i++)
		{
			if(times[i].equalsIgnoreCase(s1))
			{
				mark = true;//there an apointment
			}
			
			if(times[i].equalsIgnoreCase(s2))
			{
				mark = false; //no apointment
			}
			
			//while theres an apointment we want to mark it in the mask 
			if(mark) 
			{
				daymask[i] = false;
			}

		}
		
	}
	
	//get calendar that represents this day.
	public Calendar getCalendar()
	{
		return today;
	}
	
	//returns true or false depending on wither the internal now calendar is actually today in real life.
	public boolean isToday()
	{
		
		int y = today.get(Calendar.YEAR);
		int mo = today.get(Calendar.MONTH);
		int d = today.get(Calendar.DATE);
		Calendar t = Calendar.getInstance();
		int y1 = t.get(Calendar.YEAR);
		int mo1 = t.get(Calendar.MONTH);
		int d1 = t.get(Calendar.DATE);
		if( y == y1 && mo == mo1 && d == d1)
			return true;
		else
			return false;
	}
	
	//gets how many apoints theres is in a day
	public int getNumberOfAppointments()
	{
		return apps.size();
	}
	
	/**
	 * This just retrieves everything  ready for a JList to display
	 * @return
	 */
	public DefaultListModel getModel()
	{
		DefaultListModel dlm = new DefaultListModel();
		
		int i = 0;
		
		for(Appointment a : apps)
		{
			dlm.add(i++, a);
		}

		return dlm;
	}
	
	/**
	 *  This method just takes an array of category's and filters out everything that you don't want to see.
	 *  It will only return a list of apointments whose category is one of the categorys passed in.
	 * @param cats
	 * @return
	 */
	public DefaultListModel getModel(Category[] cats)
	{
		DefaultListModel dlm = new DefaultListModel();
		
		if(cats.length == 0)
			return this.getModel();
		
		int i = 0;
		
		for(Appointment a : apps)
		{
			for(Category c : cats)
			{
				if(c.compareTo(a.getCategory()) == 0)
				{
					dlm.add(i++, a);
				}
			}
		}
		return dlm;
	}
	
	//gets the free time as a stringarray for use with the properties dialog returns a string array
	public String[] getFreeTime()
	{
		List<String> lStr = new LinkedList<String>();
		
		for(int i = 0; i < times.length; i++)
		{
			if(!daymask[i])
			{
				lStr.add(times[i]);
			}
		}
		
		return lStr.toArray(new String[]{});
	}
	
	//override for toString() used for a dayviews Title/
	public String toString()
	{
		String days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
		
		return String.format("%02d/%02d/%d - %s",
				today.get(Calendar.DATE),
				today.get(Calendar.MONTH)+1,
				today.get(Calendar.YEAR),
				days[(today.get(Calendar.DAY_OF_WEEK) + 6) % 7]);
	}
	
	//Returns the current date as int array with {dd}{mm}{yyyy}
	public int[] getDate(){
		int[] date = new int[3];
		date[0] = this.today.get(Calendar.DAY_OF_MONTH);
		date[1] = this.today.get(Calendar.MONTH);
		date[2] = this.today.get(Calendar.YEAR);

		return date;
	}
}